package com.ruoyi.system.service.impl;

import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.DockerDisease;
import com.ruoyi.system.mapper.DockerDiseaseMapper;
import com.ruoyi.system.service.DockerDiseaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DockerMapper;
import com.ruoyi.system.domain.Docker;
import com.ruoyi.system.service.IDockerService;
import org.springframework.transaction.annotation.Transactional;
import sun.dc.pr.PRError;

import javax.annotation.Resource;

/**
 * 医生Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-26
 */
@Service
public class DockerServiceImpl implements IDockerService 
{
    @Resource
    private DockerMapper dockerMapper;
    @Resource
    private DockerDiseaseMapper dockerDiseaseMapper;

    private ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 查询医生
     * 
     * @param id 医生主键
     * @return 医生
     */
    @Override
    public Docker selectDockerById(Long id)
    {
        return dockerMapper.selectDockerById(id);
    }

    /**
     * 查询医生列表
     * 
     * @param docker 医生
     * @return 医生
     */
    @Override
    public List<Docker> selectDockerList(Docker docker)
    {
        return dockerMapper.selectDockerList(docker);
    }

    /**
     * 新增医生
     * 
     * @param docker 医生
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDocker(Docker docker)
    {
        docker.setCreateTime(DateUtils.getNowDate());
        int i1 = dockerMapper.insertDocker(docker);
        try {
            Long[] arr = objectMapper.readValue(docker.getProficientIn(), Long[].class);
            for (Long i : arr) {
                dockerDiseaseMapper.insert(docker.getId(),i);
            }
        } catch (JsonProcessingException e) {
            System.out.println("循环添加医生疾病连接表报错");
            e.printStackTrace();
        }

        return i1;
    }

    /**
     * 修改医生
     * 
     * @param docker 医生
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDocker(Docker docker)
    {
        docker.setUpdateTime(DateUtils.getNowDate());
        dockerDiseaseMapper.delete(docker.getId());
        try {
            Long[] arr = objectMapper.readValue(docker.getProficientIn(), Long[].class);
            for (Long i : arr) {
                dockerDiseaseMapper.insert(docker.getId(),i);
            }
        } catch (JsonProcessingException e) {
            System.out.println("循环添加医生疾病连接表报错");
            e.printStackTrace();
        }
        return dockerMapper.updateDocker(docker);
    }

    /**
     * 批量删除医生
     * 
     * @param ids 需要删除的医生主键
     * @return 结果
     */
    @Override
    public int deleteDockerByIds(Long[] ids)
    {
        return dockerMapper.deleteDockerByIds(ids);
    }

    /**
     * 删除医生信息
     * 
     * @param id 医生主键
     * @return 结果
     */
    @Override
    public int deleteDockerById(Long id)
    {
        return dockerMapper.deleteDockerById(id);
    }

    @Override
    public List<Docker> selectAll() {
        return dockerMapper.selectAll();
    }
}
